/**
 * Game.java
 * 
 * Created on 22.05.2012 10:43:29
 */
package de.g18.scavengerhunt.types;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.g18.scavengerhunt.AppApplicationContext;
import de.g18.scavengerhunt.utils.WebserviceUtil;

/**
 * This types stores all values of a session
 */
public class Session {

    private int        id             = 0;

    private String     name           = null;

    private int        mrXUserId      = 0;

    private int        maxPlayers     = 0;

    private int        numPlayers     = 0;

    private boolean    isRunning      = false;

    private BattleZone battleZone     = null;

    private int        timestampStart = 0;

    private DateFormat sdf            = new SimpleDateFormat("dd.MM.yyyy, HH:mm 'Uhr'");

    public Session(int id, String name, int mrXUserId, BattleZone battleZone, String startTime) {
        this.id = id;
        this.name = name;
        this.mrXUserId = mrXUserId;
        this.battleZone = battleZone;
        try {
            this.timestampStart = (int) toUnixTimeStamp(sdf.parse(startTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public Session(int id, String name, int mrXUserId, BattleZone battleZone, int timestampStart) {
        this.id = id;
        this.name = name;
        this.mrXUserId = mrXUserId;
        this.battleZone = battleZone;
        this.timestampStart = timestampStart;
    }

    private long toUnixTimeStamp(Date date) {
        return date.getTime() / 1000L;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the maxPlayers
     */
    public int getMaxPlayers() {
        return maxPlayers;
    }

    /**
     * @param maxPlayers the maxPlayers to set
     */
    public void setMaxPlayers(int maxPlayers) {
        this.maxPlayers = maxPlayers;
    }

    /**
     * @return the numPlayers
     */
    public int getNumPlayers() {
        return numPlayers;
    }

    /**
     * @param numPlayers the numPlayers to set
     */
    public void setNumPlayers(int numPlayers) {
        this.numPlayers = numPlayers;
    }

    /**
     * @return the isRunning
     */
    public boolean isRunning() {
        return isRunning;
    }

    /**
     * @param isRunning the isRunning to set
     */
    public void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    /**
     * @return the mrXUserId
     */
    public int getEvaderUserId() {
        return mrXUserId;
    }

    /**
     * @param mrXUserId the mrXUserId to set
     */
    public void setMrXUserId(int mrXUserId) {
        this.mrXUserId = mrXUserId;
    }

    /**
     * @return the BattleZone
     */
    public BattleZone getBattleZone() {
        return battleZone;
    }

    /**
     * Leave the session.
     */
    public boolean leave() {
        boolean retValue = false;
        if (WebserviceUtil.getInstance().leaveSession(AppApplicationContext.getUser().getId(), this.id)) {
            // stop the threads communicating with the webservice as there is no data to send anymore
            AppApplicationContext.getReceivePositionThread().stop();
            AppApplicationContext.getSendPositionThread().stop();

            retValue = true;
        }
        return retValue;
    }

    /**
     * @return the string value of session start time (server side)
     */
    public String getStartTime() {
        String retValue = "";
        if (timestampStart > 0) {
            // add milliseconds with *1000
            Date sessionStart = new Date((long) timestampStart * 1000);
            retValue = sdf.format(sessionStart);
        }
        return retValue;
    }
}
